<?php
	class model_home extends CI_Model
{
	function __construct()
    {
        parent::__construct();
    }
		
	function signup($customer){
		$this->db->insert("tbl_customer",$customer);
		return $this->db->insert_id();
		}	
	function check_signin($email,$password){
		$this->db->limit(1);
		$this->db->where("user_email",$email);
		$this->db->where("user_password",md5($password));
		$query = $this->db->get("tbl_users");
		if($query->num_rows() == 1){
			return $query->result();
			}
		}
	function get_user_detail($user_id){
		$this->db->limit(1);
		$this->db->where("user_id",$user_id);
		$query = $this->db->get("tbl_users");
		return $query->row();
		}
	
	function get_category(){
		$this->db->select("c1.cat_id,c2.cat_id as catid,c1.cat_name as parent, c2.cat_name as children, c2.cat_image as image, c2.cat_des as description");
		$this->db->from("tbl_category as c1");
		$this->db->join("tbl_category as c2","c1.cat_id = c2.cat_parent_id","inner");
		$this->db->where("c2.cat_status",1);
		$this->db->order_by("c2.cat_name","ASC");
		return $this->db->get();
		}	
		
	function put_user_online($user_info){
		$this->db->update("tbl_users",array("user_last_active"=> now()),"user_id = ".$user_info['id']);
		}
	
	function get_user_online($user_info){
		$this->db->where("user_last_active >", now()-10);
		$this->db->where("user_id !=",$user_info['id']);
		return $this->db->get("tbl_users");
		}
		
	function put_play_on($user_info){
		$this->db->update("tbl_users",array("user_play"=> 1),"user_id = ".$user_info['id']);
		}
		
	function search_user_matched($user_info){
		$this->db->where("mat_sp",$user_info['id']);
		return $this->db->get("tbl_match");
		}
	
	/*
	$query = $this->model_home->select_user_play();
		$play_id = $query->user_id;
		
		// create match, insert play id to table match
		$this->model_home->insert_user_play($user_id, $play_id);
		
		// change play user to not play. update play id to 0
		$this->model_home->update_user_play($play_id);
	*/
	
	function select_user_play($current_id){
		$this->db->limit(1);
		$this->db->where("user_play",1);
		$this->db->where("user_id !=",$current_id);
		$this->db->order_by("user_id","random");
		$qeury = $this->db->get("tbl_users");
		return $qeury->row();
		}
		
	function match_setup($fp, $sp, $questions){
		$this->db->insert("tbl_match",array("mat_fp"=>$fp,"mat_sp"=>$sp,"mat_questions_id"=>$questions));
		}
		
	function update_user_play($play_id,$partner_id){
		$this->db->where("user_id",$play_id);
		$this->db->or_where("user_id",$partner_id);
		$this->db->update("tbl_users",array("user_play"=> 0));
		}
		
	function set_user_ready($user_id){
		$this->db->where("user_id",$user_id);
		$this->db->update("tbl_users",array("user_ready"=>1));
		}
	function check_user_ready($user_id){
		$this->db->limit(1);
		$this->db->where("user_id",$user_id);
		$this->db->where("user_ready",1);
		$this->db->where("user_last_active >", now()-10);
		return $this->db->get("tbl_users")->num_rows();
		}
		
	function reset_($user_id){
		$this->db->where("user_id", $user_id);
		$this->db->update("tbl_users",array("user_play"=>0, "user_ready"=>0));
		}
		
	function select_questions($cat_id,$limit){
		$this->db->order_by("ques_id","random");
		$this->db->limit($limit);
		$this->db->where("ques_cat_id",$cat_id);
		return $this->db->get("tbl_question");
		}
}
?>